package com.mf.all.controller.basic;


import com.mf.all.entity.req.publicReq.ReqPublicIds;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.service.ServiceDept;
import com.mf.all.utils.UtilController;
import com.mf.all.utils.tree.UtilTree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoDic;
import com.mf.all.entity.req.basic.ReqDic;
import com.mf.all.service.ServiceDic;
import com.mf.all.entity.req.search.basic.SearchReqDic;

import java.util.List;

/**
 * 字典数据
 *
 * @date 2025-04-02 22:36:47
 */
@RestController
@RequestMapping("dic")
@Tag(name = "字典数据")
public class ControllerDic {
    @Resource
    private ServiceDic servicedic;

    @Resource
    private ServiceDept serviceDept;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("dic:search")
    public Result<Page<DtoDic>> findPage(@RequestBody SearchReqDic searchReqDic) {
        return Result.success(servicedic.findPageByParams(searchReqDic));
    }


    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("dic:search")
    public Result<List<DtoDic>> list(@RequestBody SearchReqDic searchReqDic) {

        return Result.success(servicedic.findListByParams(searchReqDic));
    }

    @Operation(summary = "列表查询(树结构)")
    @PostMapping("/listTree")
    @SaCheckPermission("dic:search")
    public Result<List<DtoDic>> listTree(@RequestBody SearchReqDic searchReqDic) {
        final List<DtoDic> listByParams = servicedic.findListByParams(searchReqDic);
        return Result.success(UtilTree.buildTree(listByParams));
    }

    /**
     * 根据部门id验证部门是否存在
     */
    private void isExistById(Integer id) {
        if (ObjectUtils.isNotEmpty(id)) {
            serviceDept.isExistById(id);
        }
    }


    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("dic:save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqDic reqDic) {
        isExistById(reqDic.getDeptId());
        servicedic.save(UtilController.setBo(reqDic, DtoDic.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("dic:update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqDic reqDic) {
        isExistById(reqDic.getDeptId());
        servicedic.updateById(UtilController.setBo(reqDic, DtoDic.class));
        return Result.success();
    }

    @Operation(summary = "删除")
    @GetMapping("/delete/{id}")
    @SaCheckPermission("dic:delete")
    public Result<Void> delete(@PathVariable("id") Integer id) {
        servicedic.deleteById(id);
        return Result.success();
    }

}
